package com.lloydfinch.composetour

import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Expand
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.lloydfinch.composetour.bean.TodoItem
import com.lloydfinch.composetour.ui.page.Counter
import com.lloydfinch.composetour.ui.page.TodoScreen
import com.lloydfinch.composetour.vm.TodoViewModel

class MainActivity : ComponentActivity() {

    private val todoViewModel by viewModels<TodoViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 原生调Compose
        //val composeView: ComposeView = findViewById(R.id.compose_view)
        //composeView.setContent { TextView() }
        Log.d("TodoScreen", "onCreate")

        for (i in 1..10) {
            todoViewModel.addItem(TodoItem("text$i", Icons.Filled.Expand))
        }

        //setContent {
        //    LayoutsCodeLabTheme {
        //        Surface {
        //            TodoActivityScreen(todoViewModel = todoViewModel)
        //        }
        //    }
        //}
        //
        //setContent {
        //    ComposeTourTheme {
        //        Column(
        //            modifier = Modifier.fillMaxSize(),
        //            verticalArrangement = Arrangement.Center,
        //            horizontalAlignment = Alignment.CenterHorizontally
        //        ) {
        //            // 添加一个Card
        //            Card(modifier = Modifier.padding(bottom = 16.dp)) {
        //                // 在Card里面添加一个CheckBox，设置边距为32dp
        //                Checkbox(checked = true, onCheckedChange = {}, modifier = Modifier.padding(32.dp))
        //            }
        //
        //            // 添加一个Button
        //            Button(onClick = { Toast.makeText(this@MainActivity, "click button", Toast.LENGTH_SHORT).show() }) {
        //                Text(text = "Button")
        //            }
        //        }
        //    }
        //}

        setContent { Counter() }
    }


    @Composable
    fun TextView() {
        Text(text = "this is a compose TextView")
        AndroidView(
            factory = { context ->
                ImageView(context)
            },
            modifier = Modifier.padding(20.dp),
            update = { imageView ->
                Log.d("Compsose", "$imageView has update")
            })
    }

    @Composable
    private fun TodoActivityScreen(todoViewModel: TodoViewModel) {
        //val items: List<TodoItem> by todoViewModel.todoItems.observeAsState(listOf())
        val items = todoViewModel.todoItems
        Log.d("TodoScreen", "activity data:${items.size}")

        TodoScreen(
            items = items,
            onAddItem = { todoViewModel.addItem(it) },
            onRemoveItem = {
                todoViewModel.removeItem(it)
                Log.d("TodoScreen", "onRemoveItem data:${items.size}")
            })
    }
}